package com.mbyte.easy.zfbPay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.zfbPay.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zte
 * @since 2021/1/28 15:16
 */
@Controller
@RequestMapping("zfbPay")
public class AlipayController {

    private String prefix = "pay/";

    @Autowired
    private AlipayService alipayService;

    @RequestMapping("index")
    public String index(){
        return prefix + "index";
    }

    /**
     * web 订单支付
     * @param WIDout_trade_no 商户订单号
     * @param WIDsubject 订单名称
     * @param WIDtotal_amount 付款金额 (实际应用中不能使用Integer , 这里仅是为了方便测试)
     * @param WIDbody 商品描述
     * @return
     * @throws Exception
     */
    @PostMapping("getPagePay")
    public String getPagePay(Model model , @RequestParam(value = "WIDout_trade_no") String WIDout_trade_no
            , @RequestParam(value = "WIDsubject") String WIDsubject
            , @RequestParam(value = "WIDtotal_amount") Integer WIDtotal_amount
            , @RequestParam(value = "WIDbody") String WIDbody ) throws Exception{
        // 模仿数据库，从后台调数据
        String pay = alipayService.webPagePay(WIDout_trade_no, WIDtotal_amount, WIDsubject);
        model.addAttribute("formContent" , pay);
        return prefix + "alipay.trade.page.pay";
    }

    /**
     * app 订单支付
     */
    @PostMapping("getAppPagePay")
    public String getAppPagePay(Model model) throws Exception{
        // 模仿数据库，从后台调数据
        String outTradeNo = "131233";
        Integer totalAmount = 1000;
        String subject = "天猫超市012";

        String pay = alipayService.appPagePay(outTradeNo, totalAmount, subject);
        model.addAttribute("formContent",pay);
        return prefix + "alipay.trade.page.pay";
    }

    /**
     * 交易查询
     */
    @PostMapping("aipayQuery")
    public AjaxResult alipayQuery() throws Exception{
        // 调取支付订单号
        String outTradeNo = "13123";

        String query = alipayService.query(outTradeNo);

        Object json = JSONObject.toJSON(query);

        return AjaxResult.success(json);
    }

    /**
     * 退款
     * @throws AlipayApiException
     */
    @GetMapping("alipayRefund")
    public AjaxResult alipayRefund(
            @RequestParam("outTradeNo")String outTradeNo,
            @RequestParam(value = "outRequestNo", required = false)String outRequestNo,
            @RequestParam(value = "refundAmount", required = false)Integer refundAmount
    ) throws AlipayApiException{

       // 调取数据
        //String outTradeNo = "15382028806591197";
        String refundReason = "用户不想购买";
        //refundAmount = 1;
        //outRequestNo = "22";

        String refund = alipayService.refund(outTradeNo, refundReason, refundAmount, outRequestNo);

        System.out.println(refund);

        return AjaxResult.success(refund);
    }

    /**
     * 退款查询
     * @throws AlipayApiException
     */
    @PostMapping("refundQuery")
    public AjaxResult refundQuery() throws AlipayApiException {

        // 调取数据
        String outTradeNo = "13123";
        String outRequestNo = "2";

        String refund = alipayService.refundQuery(outTradeNo, outRequestNo);

        return AjaxResult.success(refund);

    }

    /**
     * 交易关闭
     * @throws AlipayApiException
     */
    @PostMapping("alipayclose")
    public AjaxResult alipaycolse() throws AlipayApiException{

        // 调取数据
        String outTradeNo = "13123";

        String close = alipayService.close(outTradeNo);

        return AjaxResult.success(close);
    }
}
